<?php
/**
 * 渠道管理
 *
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class SourceController extends BaseController {
	// sourceDatalist show
	public function index(){
		$MSource = D("Admin/Source");
		$MSector = D('Admin/Sector');
		$wheres = $arr = array();
		$wheres['_string'] = "1=1";
		$psize = I('get.psize',  0,'intval');

		$psize = !$psize || $psize>100 ? $this->m_psize : $psize;
		$name = I('get.name','', 'trim');
		$company = I('get.company', -1, 'intval');

		if ($name) {
			$wheres['s.name'] = array('like', '%'.$name.'%');
		}

		if ($this->m_is_admin) {
			if ($company!=-1){
				$wheres['s.sub_company'] = array('eq', $company);
			}
		}
		else {
			$wherep = array();
			$wherep['sector_id'] = array('eq', $this->m_user['sector_id']);
			$dsector = $MSector->where( $wherep )->find();
			$psector = $dsector&&$dsector['parent_company'] ? $dsector['parent_company']  :0;
			$wheres['s.sub_company'] = array('eq', $psector);
		}


		$cnt = $MSource->alias("s")->where($wheres)->count();
		$Page = new \Common\ORG\Page( $cnt, $psize );
		$res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');
		$dlist = $MSource->alias("s")->field('s.*,se.sector_name as company')
					 		->order('update_time desc')
		              	    ->join('LEFT JOIN sector se ON se.sector_id = s.sub_company')
		              		->where($wheres)
		              		->order("s.source_id asc")
		              		->limit($Page->firstRow.','.$Page->listRows )
		              		->select();
		if( $dlist ){
			$res['rcode'] = 1;
			$res['data'] = $dlist;
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
		}else{
			$res['rcode'] = 2;
			$res['msg'] = "渠道内容为空";
		}

		e_json($res);
	}

	public function mysource()
	{
		$res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');
		$res['data'] = M("Source")->field("source_id,name")->select();
		e_json( $res );
	}

	// add source
	public function add()
	{
		$res = array('rcode'=>0,'data'=>null, 'msg'=> 'OK');
		if (IS_POST) {
			$Msource = D('Source');
			$add_name = I('post.name',false,'trim');
			$add_info = I('post.remark',false,'trim');
			$add_parent = I('post.sub_company',0,'intval');
			$addres = $Msource->add_source( $add_name, $add_info, $add_parent,$this->m_is_admin);
			if( $addres['r'] ){
				$res['rcode'] = 1;
				$res['msg'] = 'OK';
				$res['data'] = $addres['d'];
			}
			else{
				$res['msg'] = $addres['i'];
			}
		}

		e_json( $res );
	}

	public function add_edit()
	{
		$re = array('rcode'=>1,'data'=>$edit,'msg'=>'ok');
		$edit=array('sector'=>M("sector")->field("sector_id,sector_name")->select());
		echo json_encode( $re );
	}

	public function getSector()
	{
		$re = array('rcode'=>1,'data'=>NULL,'msg'=>'ok');
		$wheres = $res = array();
		$wheres['is_subcompany'] = array('eq', 1);
		$wheres['is_deleted'] = 0;
		$slist = M('sector')->field('sector_id,sector_name')->where($wheres)->select();
		if ($this->m_is_admin || 1) {
			$slist = array_reverse( $slist);
			array_push($slist, array('sector_id'=>1,'sector_name'=>'总公司'));
			$slist = array_reverse( $slist);
		}
		$re['data'] = $slist;

		e_json( $re );
	}

	// edit source records
	public function edit()
	{
		$res = array('rcode'=>-1,'msg'=>'服务器忙，请稍候再试','data'=>null);
		$Mdo = D('Admin/Source');
		if ( IS_POST ) {
			$id = I('post.source_id',false,'intval');
			$name = I('post.name','','trim');
			$parent = I('post.sub_company',false,'intval');
			$info = I('post.remark','','trim');
			$Eres =  D('Source')->channalEdit($id,$name,$parent,$info,$this->m_is_admin);
			if($Eres['r']){
				$res['rcode'] = $Eres['r'];
				$res['msg'] = $Eres['m'];
				$res['data']=$Eres['data'];
			}else{
				$res['msg'] = '编辑信息失败';
			}
		}
		else {
			$id = I('get.id', 0, 'intval');
			$wheres = array();
			$wheres['source_id'] = array('eq', $id );
			$dsecotor = $Mdo->where( $wheres )->find();
			if ($dsecotor) {
				$res['rcode'] = 1;
				$res['msg'] = 'OK';
				$res['data'] = $dsecotor;
			}
			else {
				$res['msg'] = "获取编辑数据失败";
			}
		}

		e_json($res);
	}


    //delete source records
	public function remove()
	{
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$id = I('post.id',false,'intval');
		if( !$id ){
			$res['msg'] = L('PARAM_WRONG');
		}
		else{
			$delres = D('source')->delete_sou( $id );
			if( $delres['r']){
				if ( M('source')->where(array('source_id'=>$id))->delete()) {
					$res['rcode'] = 1;
					$res['msg'] = L('DATA_DELETE_OK');
				}else{
					$res['msg'] = '删除失败';
				}
			}else{
				$res['msg'] = $delres['i'];
			}
		}
		e_json( $res );
	}
}
?>